#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <stdlib.h>
#include <direct.h>
#include <stdio.h>

int main(int argc, const char **argv)
{
	char cmdLine[4096];

	_mkdir("..\\tools\\rdistro");

	sprintf(cmdLine, "%s \"%s\" \"%s\"", argv[1], argv[2], argv[3]);

	printf("Starting RDistro process in 10 seconds...\n");
	Sleep(10000);

	printf("Copying rdistroslave" VERSION_TAG ".exe...\n");
	if(!CopyFile("..\\tools\\rdistroslave" VERSION_TAG ".exe", "..\\tools\\rdistro\\rdistroslave" VERSION_TAG ".exe", FALSE))
		printf("... failed\n");
	printf("Copying rdistrolib" VERSION_TAG ".dll...\n");
	if(!CopyFile("..\\tools\\rdistrolib" VERSION_TAG ".dll", "..\\tools\\rdistro\\rdistrolib" VERSION_TAG ".dll", FALSE))
		printf("... failed\n");
	printf("Copying luax51" VERSION_TAG ".exe...\n");
	if(!CopyFile("..\\tools\\luax51" VERSION_TAG ".exe", "..\\tools\\rdistro\\luax51" VERSION_TAG ".exe", FALSE))
		printf("... failed\n");

	printf("Launching...\n");
	printf("App: %s\n", argv[1]);
	printf("Command line: %s\n", cmdLine);

	STARTUPINFO startInfo;
	memset(&startInfo, 0, sizeof(startInfo));
	PROCESS_INFORMATION procInfo;
	if(!CreateProcessA(argv[1], cmdLine, NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &startInfo, &procInfo))
		printf(".... proc failed to start\n");

	printf("Restarted process.  Quitting in 5 seconds...\n");
	Sleep(5000);

	return 0;
}


